package com.sport.facilities.booking.system;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.sport.facilities.booking.system.httptask.InquireConfirmedTask;
import com.sport.facilities.booking.system.httptask.OnTaskCompleted;
import com.sport.facilities.booking.system.model.Booking;
import com.sport.facilities.booking.system.ui.DialogHelper;
import com.sport.facilities.booking.system.ui.YesAction;

public class InquireConfirmedBookingActivity extends Activity {
	private static final String TAG = "InquireConfirmedBookingActivity";

	final InquireConfirmedBookingActivity inqconfirmbkActivity = this;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.d(TAG, "onCreate");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.inquire_confirmed_booking);
	}

	@Override
	public void onResume() {
		super.onResume();
		fnInquireconfirmedData();
	}

	protected void fnSetBooking(List<Booking> bookingList) {
		if (bookingList.size() > 0) {

			TableLayout tbl = (TableLayout) findViewById(R.id.inqure_confirmed_booking_table);
			TextView[][] tvs;
			TableRow[] tbrs;
			tvs = new TextView[bookingList.size()][5];
			tbrs = new TableRow[bookingList.size() + 1];

			int count = bookingList.size();
			for (int i = 0; i < count; i++) {
				Booking bkk = (Booking) bookingList.get(i);
				tbrs[i] = new TableRow(this);

				if (i % 2 == 0) {
					tbrs[i].setBackgroundResource(R.color.color_white);
				} else {
					tbrs[i].setBackgroundResource(R.color.color_normal_grey);
				}


				tvs[i][0] = new TextView(this);
				tvs[i][0].setTag("date" + i);// setting tag with index i
				tvs[i][0].setText(bkk.getDate().substring(0, bkk.getDate().length() - 5));
				tvs[i][0].setTextSize(12);
				tvs[i][0].setWidth(40);
				//tvs[i][0].setBackgroundColor(Color.BLUE);
				tvs[i][0].setHeight(120);
				tvs[i][0].setGravity(Gravity.CENTER_HORIZONTAL);
				// rowspan[i].addView(tvs[i][0]);
				tbrs[i].addView(tvs[i][0]);

				tvs[i][1] = new TextView(this);
				tvs[i][1].setTag("time" + i);// setting tag with index i
				tvs[i][1].setText(bkk.getTime());
				tvs[i][1].setTextSize(12);
				tvs[i][1].setWidth(160);
				tvs[i][1].setHeight(120);
				//tvs[i][1].setBackgroundColor(Color.GREEN);
				tvs[i][1].setGravity(Gravity.CENTER_HORIZONTAL);
				// rowspan[i].setPadding(0, 20, 0, 0);
				// rowspan[i].addView(tvs[i][1]);

				tbrs[i].addView(tvs[i][1]);

				// tbrs[i].addView(rowspan[i]);

				tvs[i][2] = new TextView(this);
				tvs[i][2].setTag("centre" + i);// setting tag with index i
				tvs[i][2].setText(bkk.getCentre());
				tvs[i][2].setTextSize(12);
				tvs[i][2].setWidth(190);
				tvs[i][2].setHeight(120);
				//tvs[i][2].setBackgroundColor(Color.BLUE);
				tvs[i][2].setGravity(Gravity.CENTER_HORIZONTAL);
				tvs[i][2].setPadding(0, 20, 0, 0);
				tbrs[i].addView(tvs[i][2]);

				tvs[i][3] = new TextView(this);
				tvs[i][3].setTag("court" + i);// setting tag with index i
				tvs[i][3].setText(bkk.getCourt());
				tvs[i][3].setTextSize(12);
				tvs[i][3].setWidth(55);
				tvs[i][3].setHeight(120);
				//tvs[i][3].setBackgroundColor(Color.GREEN);
				tvs[i][3].setGravity(Gravity.CENTER_HORIZONTAL);
				tvs[i][3].setPadding(0, 20, 0, 0);
				tbrs[i].addView(tvs[i][3]);

				tvs[i][4] = new TextView(this);
				tvs[i][4].setTag("activity" + i);// setting tag with index i
				String tmpActivity = bkk.getActivity().replaceAll("  ", "\n");
				tmpActivity = tmpActivity.replaceAll(" ", "\n");
				tvs[i][4].setText(tmpActivity);
				tvs[i][4].setTextSize(12);
				tvs[i][4].setWidth(130);
				tvs[i][4].setHeight(120);
				//tvs[i][4].setBackgroundColor(Color.YELLOW);
				tvs[i][4].setGravity(Gravity.CENTER_HORIZONTAL);
				tvs[i][4].setPadding(0, 20, 0, 0);
				tbrs[i].addView(tvs[i][4]);

				tbl.addView(tbrs[i]);
			}
			tbrs[count] = new TableRow(this);
			tbrs[count].setBackgroundResource(R.color.color_deep_blue);
			TextView emptytvs;
			emptytvs = new TextView(this);

			emptytvs = new TextView(this);
			emptytvs.setTag("empty");// setting tag with index i
			emptytvs.setText("");
			emptytvs.setTextSize(10);
			emptytvs.setWidth(100);
			emptytvs.setHeight(30);
			emptytvs.setGravity(Gravity.CENTER);
			tbrs[count].addView(emptytvs);
			tbl.addView(tbrs[count]);

		} else {
			//DialogHelper.getOkAlertDialog(this, "No record has been found.", "").show();
			
			
			YesAction yesAction = new YesAction() {
				public void fnDoYesAction() {
					finish();
				}
			};
			
			DialogHelper
			.getOkActionDialog(
					inqconfirmbkActivity,
					"Inquire Confirmed Booking",
					"No record has been found.",
					yesAction)
			.show();
			
		}
	}

	private void fnInquireconfirmedData() {
		
		//remove old table rows for resume()
		removetbr();
		
		final InquireConfirmedBookingActivity icmActivity = this;
		OnTaskCompleted completeAction = new OnTaskCompleted() {
			public void onTaskCompleted(Object resultList) {
				Log.d(TAG,
						"onTaskCompleted, Booking : " + resultList.toString());
				List<Booking> bookingList = (List<Booking>) resultList;
				icmActivity.fnSetBooking(bookingList);
			}

			public void onTaskFailed(String errMessage) {
				Log.e(TAG, errMessage);
				DialogHelper.getOkAlertDialog(icmActivity, "Error In Loading Booking", errMessage).show();
			}
		};
		InquireConfirmedTask icbTask = new InquireConfirmedTask(this,
				completeAction);
		icbTask.fnGetConfirmedList();
	}
	
	
	public void removetbr() {

		//remove table rows for reload
		TableLayout tbl = (TableLayout) findViewById(R.id.inqure_confirmed_booking_table);		
		int count = tbl.getChildCount();
		for (int i = 1; i < count; i++) {
		    View child = tbl.getChildAt(i);
		    if (child instanceof TableRow) ((ViewGroup) child).removeAllViews();
		}
	}
	
	
}
